/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sigl;

import sigl.exporter.GDF;
import sigl.model.Model;
import sigl.updater.Updater;

/**
 *
 * @author uxmal
 */
public class SIGLMain {

    /**
     * @param args the command line arguments
     */
    public static int main(String[] args) {
        // TODO code application logic 

        if (args.length <= 1) {
            System.out.println("Use: sigl create|update|export [args]");

        }
         // update input.yml list.txt output.yml cache/ 
        if (args[0].equalsIgnoreCase("create")) {

            String 
                    cacheDirectory = "/home/uxmal/cpan/cache/",
                    downloadList = "/home/uxmal/cpan/xau",
                    outputFile = "testdata/default.yml";


            if (args.length > 1) {
                downloadList = args[1];
            }
            if (args.length > 2) {
                outputFile = args[2];
            }
            if (args.length > 3) {
                cacheDirectory = args[3];
            }

            System.out.println("cacheDirectory:" + cacheDirectory);
            System.out.println("downloadList:" + downloadList);
            System.out.println("outputFile:" + outputFile);

            System.out.print("Initialisation des données...");
            
            Model model = new Model();
            Updater updater = new Updater(model, cacheDirectory);
            updater.updateFromFile(downloadList);
            System.out.println("Mise à jour terminée.");
            // mise à jour du projet
            System.out.println(" - Packages : " + model.getPackageSize());
            System.out.println(" - Modules  : " + model.getModuleSize());
            //model.saveAs(outputFile);
            System.out.println(" - Fermeture..");
            return 0;

        }// update input.yml list.txt output.yml cache/ 
        else if (args[0].equalsIgnoreCase("update")) {

            String inputFile = "testdata/default.yml",
                    cacheDirectory = "/home/uxmal/cpan/cache/",
                    downloadList = "/home/uxmal/cpan/default",
                    outputFile = "testdata/default.yml";

            if (args.length > 1) {
                inputFile = args[1];
            }
            if (args.length > 2) {
                downloadList = args[2];
            }
            if (args.length > 3) {
                outputFile = args[3];
            }
            if (args.length > 4) {
                cacheDirectory = args[4];
            }

            System.out.println("cacheDirectory:" + cacheDirectory);
            System.out.println("downloadList:" + downloadList);
            System.out.println("inputFile:" + inputFile);
            System.out.println("outputFile:" + outputFile);

            System.out.print("Mise à jour des données...");
            
            Model model = new Model(inputFile);
            Updater updater = new Updater(model, cacheDirectory);
            updater.updateFromFile(downloadList);
            System.out.println("Mise à jour terminée.");
            // mise à jour du projet
            System.out.println(" - Packages : " + model.getPackageSize());
            System.out.println(" - Modules  : " + model.getModuleSize());
            //model.saveAs(outputFile);
            System.out.println(" - Fermeture..");
            return 0;

        } else if (args[0].equalsIgnoreCase("export")) {

            String mode = "packages";
            String inputFile = "testdata/default.yml";
            String outputFile = "testdata/" + mode + ".gdf";

            if (args.length > 1) {
                mode = args[1];
            }
            if (args.length > 2) {
                inputFile = args[2];
            }

            if (args.length > 3) {
                outputFile = args[3];
            }
            /*
            boolean noModules = false;
            if (args.length > 4) {
                if  (args[4].equalsIgnoreCase("packagesOnly")) noModules = true;
            }*/
            System.out.println("mode:" + mode);
            System.out.println("inputFile:" + inputFile);
            System.out.println("outputFile:" + outputFile);
            
            GDF exporter = new GDF(new Model(inputFile));
            if (mode.equalsIgnoreCase("modules")) {
                exporter.exportModules(outputFile);
            }
            else if (mode.equalsIgnoreCase("packagesOnly")) {
                exporter.exportPackages(outputFile, true);
            }
            else if (mode.equalsIgnoreCase("packages")) {
                exporter.exportPackages(outputFile, false);
            }
            else {
                System.out.println("mauvais argument !");
                return 1;
            }

            return 0;

        } else {
            System.out.println("Arguments: (entree.yml) repertoire_cache/ packages.txt sortie.yml");

        }

        return 1;

    }
}
